//
//  MessageViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/8.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class MessageViewController: BaseViewController {

    var heightArr: [CGFloat]?
    var messageArr: [String]?
    
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        createUI()
        initBaseLayout()
        layoutPageSubviews()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response
    override func rightItemAction(sender: UIButton) {
        
        
        let alertController = CustomAlertViewController()
        alertController.showActionSheet(controller: self, destructiveTitle: "全部标记为已读", cancelTitle: "取消")

    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(messageTableView)
    }
    
    func layoutPageSubviews() {
        messageTableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }
    func createUI() {
        initNavItem(controller: self, imageName: "nav_back_n", highlitedImageName: "nav_back_h", selectedImageName: "", isLeft: true)
        
        initNavItem(controller: self, imageName: "nav_edit_n", highlitedImageName: "nav_edit_h", selectedImageName: "nav_finish_n", isLeft: false)
        self.title = "消息中心"
        
        heightArr = [CGFloat]()
        messageArr = ["【台州市】快件已到达 浙江台州分拨中心", "活动期间为您准备了10积分的新人奖励，可以抵现的哟！", "活动期间为您准备了10积分的新人奖励，可以抵现的哟！活动期间为您准备了10积分的新人奖励，可以抵现的哟！", "【台州市】快件已到达 浙江台州分拨中心", "DS SEBAGH/赛贝格 臻颜无暇精粹5ml，该商品已售完"]
        for i in 0 ..< messageArr!.count {
            let rect = (messageArr![i] as NSString).boundingRect(with: CGSize(width: SCREEN_WIDTH-28, height: 99999), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)], context: nil)
            
            heightArr?.append(rect.size.height)
        }
    }
    
    // MARK: - setters and getters
    var _messageTableView: UITableView!
    var messageTableView: UITableView {
        if _messageTableView == nil {
            _messageTableView = UITableView(frame: CGRect.zero, style: .grouped)
            _messageTableView.backgroundColor = UIColor(hexString: "#E5E5E5")
            _messageTableView.delegate = self
            _messageTableView.dataSource = self
            _messageTableView.register(MessageCell.self, forCellReuseIdentifier: "MessageCell")
        }
        return _messageTableView
    }

}

extension MessageViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let string = "MessageCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: string, for: indexPath) as! MessageCell
        
        cell.configUI(messageArr: messageArr!, indexPath: indexPath)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return heightArr![indexPath.section]+100
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 16
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
}
